<?php
class User_lottery_model extends BZ_Model
{
    protected $table   = 'user_lottery';
    protected $table_1 = 'user';

	/**
     * 查询某抽奖活动的参与用户数
     * @param unknown $lottery_id
     */
    public function total($params)
    {
        $this->db->select('*');
		$this->db->from($this->table.' AS user_lottery');
        if ( !empty($params['lottery_id'])) {
            $this->db->where_in('user_lottery.lottery_id', $params['lottery_id']);
        }

        if ( !empty($params['user_name'])) {
            $this->db->like('user_lottery.user_name', $params['user_name'], 'after');
        }

		return $this->db->count_all_results();
    }

	/**
     * 查询某抽奖活动的所有参与用户
	 * @param number $page_num
	 * @param number $num
     * @param unknown $lottery_id
     */
    public function page_list($page_num, $num, $params)
    {
		$this->db->select('id,uid,lottery_id,date,weight,today_num,created_at, today_total,updated_at, user_name');
		$this->db->from($this->table.' AS user_lottery');
		if ( !empty($params['lottery_id'])) {
            $this->db->where_in('user_lottery.lottery_id', $params['lottery_id']);
        }

        if ( !empty($params['user_name'])) {
            $this->db->like('user_lottery.user_name', $params['user_name'], 'after');
        }

		$this->db->order_by('user_lottery.id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    /**
     * 注册用户统计
     */
    public function statisticsTotal($params)
    {
        $this->db->select('COUNT(DISTINCT uid) AS total');
        $this->db->from($this->table);
        $this->db->where_in('lottery_id', $params['lottery_id']);
        if (!empty($params['startDate'])) {
           $this->db->where('user_lottery.created_at >', $params['startDate'].' 00:00:00');
        }

        if (!empty($params['endDate'])) {
            $this->db->where('user_lottery.created_at <=', $params['endDate'].' 23:59:59');
        }
        if ( !empty($params['user_name'])) {
            $this->db->like('user_name', $params['user_name']);
        }
        return $this->db->get()->row()->total;
    }

    /**
     * 注册用户统计
     */
    public function statisticsList($page_num, $num, $params)
    {
        $query = array();
        $lotteryIds = is_array($params['lottery_id']) ? implode(',', $params['lottery_id']) : '';
        $sql = 'SELECT COUNT(id) AS `count`, SUM(`today_total`) AS todayTotal, SUM(today_num) AS numTotal, SUM(weight) AS weightTotal,uid, user_name,created_at FROM ';
        $sql .= '( SELECT id, uid,today_total, today_num,weight, created_at,user_name FROM user_lottery ';
        $sql .= 'WHERE lottery_id IN ('. $lotteryIds. ') ';
        if ( !empty($params['startDate'])) {
            $sql .= ' AND created_at > ? ';
            array_push($query, $params['startDate'].' 00:00:00 ');
        }
        if ( !empty($params['endDate'])) {
            $sql .= ' AND created_at < ? ';
            array_push($query, $params['endDate'].' 23:59:59');
        }
        if ( !empty($params['user_name'])) {
            $sql .= ' AND user_name LIKE ? ';
            array_push($query, '%'.$params['user_name']. '%');
        }
        $sql .= 'ORDER BY id DESC) AS user_lottery GROUP BY uid ORDER BY `count` DESC ';
        $sql .= 'LIMIT '. $page_num. ' OFFSET '. $num;
        return $this->db->query($sql, $query);
    }

}